package com.mindata.blockchain.common;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Create by Wei on 2018/5/22
 * 时间工具类
 */
public class DateUtils {

    public static String frontCompWithZore(int sourceDate,int formatLength) {
        String newString = String.format("%0" + formatLength + "d", sourceDate);
        return newString;
    }

    //将date 转成string格式
    public static String dateToString(Date date,String reg) {
        if(date!=null){
            String datestr = null;
            SimpleDateFormat df = new SimpleDateFormat(reg);
            datestr = df.format(date);
            return datestr;
        }else{
            return  null;
        }

    }


    //将date 转成string并使用20180607的格式
    public static String dateToString(Date date) {
        String datestr = null;
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        datestr = df.format(date);
        return datestr;
    }

    //将date 转成string并使用2018-06-07的格式
    public static String dateToStringAndGang(Date date) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        return df.format(date);
    }



    //根据系统时间+传入时间获得所有区间日期 包括星期
    public static String[] getDates(String day,String booleanToday){
        int initI = 0;
        int dayInt =  Integer.parseInt(day);
        if("否".equals(booleanToday)){
            initI=1;
        }
        Date date = new Date();
        String dates[] = new String[dayInt];
        String datesReturn[];
        for (int i =initI; i <dayInt;i++) {
            Date dateNew = org.apache.commons.lang3.time.DateUtils.addDays(date, i);
            dates[i] =   DateFormatUtils.format(dateNew,"yyyy年MM月dd日(E)");
        }
        if("否".equals(booleanToday)){
            datesReturn=new String[dayInt-1];
            System.arraycopy(dates, 1, datesReturn, 0, datesReturn.length);
            return datesReturn;

        }
        return dates;
    }

    //根据传入的时间计算中间有多少天
    public static String[] getDates(String yyrq, String endYyrq,String pattern) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(yyrq));
        long time1 = cal.getTimeInMillis();
        cal.setTime(sdf.parse(endYyrq));
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);
        //return getDates(String.valueOf(between_days));
        int dayInt = Integer.parseInt(String.valueOf(between_days));
        String dates[] = new String[dayInt+1];
        Date dateBegin = org.apache.commons.lang3.time.DateUtils.parseDateStrictly(yyrq, "yyyy-MM-dd");
        for (int i = 0; i <=dayInt ;i++) {
            Date dateNew = org.apache.commons.lang3.time.DateUtils.addDays(dateBegin, i);
            dates[i] =   DateFormatUtils.format(dateNew,pattern);
        }
        return dates;
    }

    /**
     *@描述  获取给定天数+J后的天数str 格式2018-07-01
     *@参数  [day, j]
     *@返回值  java.lang.String
     *@创建人  Wei
     *@创建时间  2018/9/15
     */
    public static String getAddDate(String day, int int_after_day) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String dateAfter = "";
        try {
            long time = format.parse(day).getTime();
            long time2 =time + int_after_day*24*60*60*1000L;
            Date date = new Date();
            date.setTime(time2);
            dateAfter = format.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateAfter;
    }


    /**
     *@描述  根据初始日期和结束日期计算中间相差的天数
     *@参数  [date1, date2]
     *@返回值  相差的天数
     *@创建人  Wei
     *@创建时间  2018/9/15
     */
    public static int differentDaysByMillisecond(String date1,String date2)
    {
        int days = 0;
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date();
            long timeStar = format.parse(date1).getTime();
            long timeEnd = format.parse(date2).getTime();
            days = (int) ((timeStar - timeEnd) / (1000*3600*24));
        }catch (ParseException e) {
            e.printStackTrace();
        }
        return days;
    }

    /**
     * 判断一个时间是否在一个时间段内
     * @param nowTime
     * @param beginTime
     * @param endTime
     * @return
     */
    public static boolean belongCalendar(Date nowTime, Date beginTime,Date endTime){

        if(nowTime.getTime()<beginTime.getTime()){
            return false;
        }
        if(nowTime.getTime()>endTime.getTime()){
            return false;
        }

        return true;
    }



    /**
     * 指定时间前的N天
     *
     * @param date
     * @return
     */
    public static Date getSubDay(Date date, int dayNum) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        //调用Calendar类中的add()，增加时间量
        calendar.add(Calendar.DATE, -dayNum);
        return calendar.getTime();
    }

    /**
     * 指定时间后的N天
     *
     * @param date
     * @return
     */
    public static Date getAddDay(Date date, int dayNum) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        //调用Calendar类中的add()，增加时间量
        calendar.add(Calendar.DATE, dayNum);
        return calendar.getTime();
    }

    /**
     * 日期解析
     * @param date  日期
     * @param pattern  格式，如：DateUtils.DATE_TIME_PATTERN
     * @return  返回Date
     */
    public static Date parse(String date, String pattern) {
        try {
            return new SimpleDateFormat(pattern).parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
